// ResourceImage.cpp: implementation of the ResourceImage class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ResourceImage.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

IMPLEMENT_DYNAMIC(ResourceImage, Image)

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

ResourceImage::ResourceImage(HINSTANCE hinst, LPCTSTR name, UINT type, UINT flags)
{
	m_hImage = NULL;
	m_hinst = hinst;
	m_name = (LPTSTR)malloc((lstrlen(name)+1) * sizeof(TCHAR));
	lstrcpy(m_name, name);
	m_type = type;
	m_flags = flags;
	m_bName = TRUE;
	loadImage();
}

ResourceImage::ResourceImage(HINSTANCE hinst, int id, UINT type, UINT flags)
{
	m_hImage = NULL;
	m_hinst = hinst;
	m_name = MAKEINTRESOURCE(id);
	m_type = type;
	m_flags = flags;
	m_bName = FALSE;
	loadImage();
}

ResourceImage::~ResourceImage()
{
	if (m_bName)
		free(m_name);
}

BOOL ResourceImage::loadImage()
{
	m_hImage = ::LoadImage(m_hinst, m_name, m_type, 0, 0, m_flags);
	return (m_hImage != NULL);
}

void ResourceImage::paint(CDC* pDC, AWTCanvas* canvas)
{
	if (m_hImage == NULL)
		return;
	HDC hMemDC;
	hMemDC = ::CreateCompatibleDC(pDC->m_hDC);
	BITMAP bm;
	::GetObject(m_hImage, sizeof(BITMAP), (LPSTR)&bm);
	
  HBITMAP hbmpOld = (HBITMAP)::SelectObject(hMemDC, m_hImage);
	BitBlt(pDC->m_hDC, 0, 0, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY);
  ::SelectObject(hMemDC,hbmpOld);
}

CSize ResourceImage::getSize()
{
	if (m_hImage == NULL)
		return CSize(0,0);
	CSize sz;
	BITMAP bm;
	::GetObject(m_hImage, sizeof(BITMAP), (LPSTR)&bm);
	sz.cx = bm.bmWidth;
	sz.cy = bm.bmHeight;
	return sz;
}
